package com.uh.calculation.util;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {


    //比较月份差距 Date d1为较大月份
    public static int getMonthDiff(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        if(c1.getTimeInMillis() < c2.getTimeInMillis()) return 0;
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        int yearInterval = year1 - year2;
        if(month1 < month2 || month1 == month2 && day1 < day2) yearInterval --;
        int monthInterval = (month1 + 12) - month2 ;
        if(day1 < day2) monthInterval --;
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }


    public  static Boolean  bj (Date lastTime ,String timeCode,String   year_is_month,Boolean is_hc) {
        //时间判断
        if (lastTime != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                Date d = sdf.parse(timeCode);
                if (d.before(lastTime)) {
                    return false;
                }
            } catch (Exception e) {
            }
        }
        //是否有核验规则
        if(is_hc){
            if(StringUtils.isEmpty(year_is_month)) {
                return true;
            }else {
                return TimeUtil.timebj(year_is_month, timeCode);
            }
        }
        return  true;
    }

    //判断d1 比d2 大
    public static Boolean  timebj(String d1,String d2) {
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date D1= sdf.parse(d1);
            Date D2= sdf.parse(d2);
            if (D1.getTime() > D2.getTime()) {
                return  true;
            }else{
                return  false;
            }
        }catch (Exception e){}
        return  false;
    }
}